import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { DayRechargeModule } from "../../../modules/DayRechargeModule";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { WindowEvent } from "../../component/window/WindowEvent";
import { DayRechargeHintBtn } from "../view/dayAndAccumulateRecharge/DayRechargeHintBtn";
import { EHolidayRetGiftBagStatus } from "../../../Message/Public/EHolidayRetGiftBagStatus";
import { EffectManager } from "../manager/EffectManager";
import { FilterConst } from "../../component/gconst/FilterConst";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { SNewDayRechargeInfo } from "../../../Message/Public/SNewDayRechargeInfo";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { DayAndAccumulateRenderInfo } from "../view/dayAndAccumulateRecharge/DayAndAccumulateRenderInfo";
import { GameProxy } from "../mvc/GameProxy";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ClockManager } from "../manager/ClockManager";
import { IDayRechargeModule } from "../../../modules/interfaces/IDayRechargeModule";
//class DayRechargeController
    
    export  class DayRechargeController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._dayModule) 
            {
                this._dayModule = new DayRechargeModule(EActiveType._EActiveTypeNewDayRecharge1);
                this._dayModule.addEventListener(WindowEvent.SHOW, this.addListener);
                this._dayModule.addEventListener(WindowEvent.CLOSE, this.delListener);
            }
            return this._dayModule;
        }

        /* internal  */get hintBtn(): DayRechargeHintBtn
        {
            if (!this._hintBtn) 
            {
                this._hintBtn = new DayRechargeHintBtn();
            }
            return this._hintBtn;
        }

        /* internal  */hitntBtnDisplay(): void
        {
            return;
        }

        /* internal  */checkHintBtnShine(): void
        {
            var loc1=false;
            var loc2=0;
            if (!cache.role.dayRechargeInfo) 
            {
                return;
            }
            var loc3=0;
            var loc4=cache.role.dayRechargeInfo.status;
            for(loc2 of loc4) 
            {
                if (loc2 != EHolidayRetGiftBagStatus._EHolidayRetGiftBagAvailable) 
                {
                    continue;
                }
                loc1 = true;
                break;
            }
            if (loc1) 
            {
                EffectManager.glowFilterReg(this.hintBtn, [FilterConst.guideTipsFilter]);
            }
            else if (this._hintBtn) 
            {
                EffectManager.glowFilterUnReg(this._hintBtn);
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeNewDayRecharge, this.onActiveTypeNewDayRecharge);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateNewDayRechargeInfo, this.onNewDayRechargeInfo);
            Dispatcher.addEventListener(EventName.getDayRechargeInfoBack, this.getDayRechargeInfoBack);
            Dispatcher.addEventListener(EventName.getDayRechargeInfoBackSuccess, this.ongetDayRechargeInfoBackSuccess);
            Dispatcher.addEventListener(EventName.GetDayRechangeAwardSuccess, this.onGetAwardsHandler);
            return;
        }

        /* internal  */onGetAwardsHandler(arg1: DataEvent): void
        {
            this.isGetAllHandler();
            return;
        }

        /* internal  */isGetAllHandler(): Boolean
        {
            var loc1=this.cache.role.isRechangeGiftGetAll();
            if (loc1) 
            {
                if (this.hintBtn) 
                {
                    this.hintBtn.hide();
                }
                if (!this.view.isHide) 
                {
                    this.view.hide();
                }
            }
            return loc1;
        }

        /* internal  */isActiveOpen(): Boolean
        {
            var loc1=cache.active.isActiveInEffect(EActiveType._EActiveTypeNewDayRecharge1);
            var loc2=cache.active.isActiveInEffect(EActiveType._EActiveTypeNewDayRecharge2);
            return loc1 || loc2;
        }

        /* internal  */ONshowDAYRECHARGEmODULEtEST(arg1: DataEvent): void
        {
            var loc1=new SNewDayRechargeInfo();
            loc1.activeType = 1249;
            var loc2=new Map<any, any> /* flash.utils.Dictionary */();
            var loc3=new Map<any, any> /* flash.utils.Dictionary */();
            var loc4=new Map<any, any> /* flash.utils.Dictionary */();
            loc2[8] = 44011012;
            loc2[88] = 44011012;
            loc2[888] = 44011012;
            loc2[6666] = 44011012;
            loc2[8888] = 44011012;
            loc2[9999] = 44011012;
            loc3[8] = 888;
            loc3[88] = 8888;
            loc3[888] = 88888;
            loc3[6666] = 88888;
            loc3[8888] = 88888;
            loc3[9999] = 88888;
            loc4[8] = 0;
            loc4[88] = 1;
            loc4[888] = 2;
            loc4[6666] = 2;
            loc4[8888] = 2;
            loc4[9999] = 2;
            loc1.update = true;
            loc1.status = loc4;
            loc1.giftCode = loc2;
            loc1.value = loc3;
            cache.role.dayRechargeInfo = loc1;
            if (loc1) 
            {
                if (loc1.update) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(79019));
                }
                if (view.isHide) 
                {
                    view.show();
                    this._dayModule.updateGiftList(this.dealSeverInfo(loc1));
                }
            }
            view.show();
            return;
        }

        /* internal  */ongoGetDayRechargeGift(arg1: DataEvent): void
        {
            var loc1=arg1.data as DayAndAccumulateRenderInfo;
            if (loc1.activeType == EActiveType._EActiveTypeNewDayRecharge1 || loc1.activeType == EActiveType._EActiveTypeNewDayRecharge2) 
            {
                GameProxy.roleProxy.getNewDayRechargeGiftBag(loc1.activeType, loc1.targetNum);
            }
            return;
        }

        /* internal  */getDayRechargeInfoBack(arg1: DataEvent): void
        {
            if (this._activeToClient) 
            {
                GameProxy.roleProxy.viewNewDayRechargeGiftBag(this._activeToClient.code);
            }
            return;
        }

        /* internal  */onNewDayRechargeInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SNewDayRechargeInfo;
            cache.role.dayRechargeInfo = loc1;
            this.checkHintBtnShine();
            if (!view.isHide) 
            {
                this._dayModule.updateGiftList(this.dealSeverInfo(loc1));
            }
            var loc2=this.isGetAllHandler();
            return;
        }

        /* internal  */ongetDayRechargeInfoBackSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data as SNewDayRechargeInfo;
            cache.role.dayRechargeInfo = loc1;
            if (loc1) 
            {
                if (loc1.update) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(79019));
                }
                if (view.isHide) 
                {
                    this.checkHintBtnShine();
                    view.show();
                    this._dayModule.updateGiftList(this.dealSeverInfo(loc1));
                }
            }
            var loc2=this.isGetAllHandler();
            return;
        }

        /* internal  */dealSeverInfo(arg1: SNewDayRechargeInfo): Array<any>
        {
            var loc2=null;
            var loc3=null;
            var loc4=0;
            var loc5=null;
            var loc6=0;
            var loc7=0;
            var loc1=[];
            if (arg1) 
            {
                loc2 = [];
                var loc8=0;
                var loc9=arg1.giftCode;
                for (loc3 in loc9) 
                {
                    loc2.push(loc3);
                }
                loc2.sort(Array.NUMERIC);
                loc4 = loc2.length;
                loc7 = 0;
                while (loc7 < loc4) 
                {
                    loc6 = loc2[loc7];
                    (loc5 = new DayAndAccumulateRenderInfo()).activeType = arg1.activeType;
                    loc5.targetNum = loc6;
                    loc5.giftItemCode = arg1.giftCode[loc6];
                    loc5.targetValue = arg1.value[loc6];
                    loc5.giftState = arg1.status[loc6];
                    loc5.todayNum = arg1.todayNum;
                    loc1.push(loc5);
                    ++loc7;
                }
            }
            return loc1;
        }

        /* internal  */onActiveTypeNewDayRecharge(arg1: Object=null): void
        {
            var loc1=false;
            this._activeToClient = arg1 as SActiveToClient;
            if (this.isActiveOpen()) 
            {
                loc1 = this.cache.role.isRechangeGiftGetAll();
                if (!loc1) 
                {
                    ClockManager.instance.addDateCall(this._activeToClient.endDt, this.removeActive);
                    this.hintBtn.show();
                    this.checkHintBtnShine();
                }
            }
            else if (this._hintBtn) 
            {
                this._hintBtn.hide();
            }
            return;
        }

        /* internal  */removeActive(): void
        {
            this.hintBtn.hide();
            if (!view.isHide) 
            {
                view.hide();
            }
            return;
        }

        /* internal  */addListener(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.addEventListener(EventName.goGetDayRechargeGift, this.ongoGetDayRechargeGift);
            return;
        }

        /* internal  */delListener(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.removeEventListener(EventName.goGetDayRechargeGift, this.ongoGetDayRechargeGift);
            return;
        }

        private /* var */_dayModule: IDayRechargeModule;

        private /* var */_hintBtn: DayRechargeHintBtn;

        private /* var */_activeToClient: SActiveToClient;
    }
